package com.whitedust.top60;

import com.whitedust.structure.ListNode;

/**
 *
 * @author A
 * @since 2025/11/10 16:36
 */

public class Top51 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode();
        ListNode pre = dummy;
        int add = 0;
        while (l1 != null && l2 != null) {
            ListNode cur = new ListNode();
            int sum = l1.val + l2.val + add;
            add = sum / 10;
            cur.val = sum % 10;
            pre.next = cur;
            pre = cur;
            l1 = l1.next;
            l2 = l2.next;
        }
        if (l1 == null) {
            while (l2 != null) {
                ListNode cur = new ListNode();
                int sum = l2.val + add;
                add = sum / 10;
                cur.val = sum % 10;
                pre.next = cur;
                pre = cur;
                l2 = l2.next;
            }
        } else if (l2 == null) {
            while (l1 != null) {
                ListNode cur = new ListNode();
                int sum = l1.val + add;
                add = sum / 10;
                cur.val = sum % 10;
                pre.next = cur;
                pre = cur;
                l1 = l1.next;
            }
        }
        if (add == 1) {
            pre.next = new ListNode(1);
        }
        return dummy.next;
    }
}
